class AdminController < ApplicationController
  def index
    clean_session
    @order = Order.new    
  end
  #根据车种选择不同来改变车辆大小选择框内的待选项
  def display_size_column
    car_type = params[:car_type]
    car_sizes = Typesize.find(:all,:select => "car_size", :conditions => ["car_type = ?",car_type])
    render :update do |page|
      page.replace_html "size_column", :partial => "size", :locals => {:car_sizes => car_sizes}
    end
  end
  
  def getfile
    Order.get_orders_file
    Order.get_recipes_file
    Order.get_parts_file
    Order.get_partTasks_file
    Order.get_partRelations_file  
    flash.now[:notice] = "ファイルを生成しました！"
  end
  
  
  # 保存order信息，并且在session中创建position和order_item，然后进入诊断
  
  def create_order
    @order = Order.new(params[:order])
    if @order.save
      session[:order] = @order.id
      session[:position] = nil
      session[:order_item] = OrderItem.new
      redirect_to :action => :direction
    else
      render :index
    end
  end
  
  #8.28 ajax选中部位后把position的id传过来放入session中,并且检查该部位有无左右之分，以及验证该部位是否已经被诊断。
  
  def check_position
    @order_item = get_order_item
    
    #页面中选择的部位
    @selected_position = Position.find(params[:position])
    
    #positions是order中已经诊断过的所有部位
    positions = get_order.positions
    
    #指示部位是否已存在的变量
    @position_exist = false
    positions.each{|position| 
      if position == @selected_position
        @position_exist = true  
        end}
      unless @position_exist
        session[:position] = params[:position]
        @side_of_position = params[:side_of_position]
        
        #把左右信息加入order_item的side属性里
        @order_item.side = @side_of_position
        position = get_position
        @flag = position.twosides
      end
      
    end
    
    #8.28 ajax在部位选择框内的选项发生变化时，自动验证该部位是否已经被诊断，并且判断是否显示左右选项
    
    def check_position_on_change 
      order_item = get_order_item
      selected_position = Position.find(params[:position])
      positions = get_order.positions
      position_exist = false
      positions.each{|position| 
        if position == selected_position
          position_exist = true  
          end}
        unless position_exist
          if selected_position.twosides
            render :update do |page|
              page.replace_html("side_option",:partial => "select_side",:locals => {:side => order_item.side})
              page[:side_option].visual_effect :blind_down
            end
          else
            render :update do |page|
              page[:side_option].hide
            end
          end
        else
          render :update do |page|
            page.alert "#{selected_position.part}の部位がすでに診断されました。ほかの部位を選んでください"
          end
        end
      end
      
      def direction
        order_item = get_order_item
        @direction = order_item.direction
        if request.post?
          #8.20 start 把损伤角度信息加入order_item当中
          order_item = get_order_item
          order_item.direction = params[:direction]
          #end
          redirect_to :action => :select_position, :direction => params[:direction]
        end
      end
      
      #进行下一个部位的诊断
      def next_position
        session[:position] = nil
        session[:order_item] = OrderItem.new
        redirect_to :action => :direction
      end
      
      def select_position
        
        #把session中的position_id放入@position中，以便在回退时显示过去已经选择的部位
        @position = session[:position]
        direction = params[:direction]
        order = get_order
        
        #根据传入的direction和order在Position数据库中寻找匹配的部位
        @positions = Position.get_positions(direction,order)
      end
      
      #从order浏览页面进入的情况下将传入order的id
      def show_info
        session[:order_item] = nil
        if params[:id]
          session[:order] = params[:id]
        end  
        begin
          @order = get_order
        rescue Exception
          redirect_to :action => :index and return
        end
        
        #计算合计时间值
        @order.count_sum_index
        @order_items = @order.order_items
        
        #以下分别是四个不同指数的时间参数
        @time_index_of_paint = @order.time_index_of_paint
        @time_index_of_uninstall = @order.time_index_of_uninstall
        @time_index_of_surface = @order.time_index_of_surface
        @time_index_of_putty = @order.time_index_of_putty
      end
      
      #删除order_item元素时更新整个list元素，并且在total_item块使用黄渐变效果
      def destroy_order_item
        order = get_order
        order_item = OrderItem.find(params[:id])
        order.order_items.delete(order_item)
        #计算合计值
        order.count_sum_index       
        #以下是ajax页面更新部分
        render :update do |page|
          #如果外板order_items的个数为零的话，就让内板诊断的按钮失效
          if order.num_of_out_order_items  == 0
            page[:submit].disabled = true
          end
          page.remove "order_item_#{order_item.id}"
          page.replace_html("order_item",:partial => "order_items",:locals => {:order_items => order.order_items,
                                                         :order => order,
                                                         :time_index_of_uninstall => order.time_index_of_uninstall,
                                                         :time_index_of_putty => order.time_index_of_putty,
                                                         :time_index_of_surface => order.time_index_of_surface, 
                                                         :time_index_of_paint => order.time_index_of_paint})
          page[:total_time].visual_effect :highlight,
                                      :startcolor => "#88ff88",
                                      :endcolor => "#114411"
        end
      end
      
      def edit_order_item
        
        #在编辑order_item时，首先将待编辑的order_item存入session
        order_item = OrderItem.find(params[:id])
        session[:order_item] = order_item
        
        #外板，内板order_item进入不同的action
        if order_item.position
          session[:position] = order_item.position
          redirect_to :controller => :damage_judge
        else
          redirect_to :controller => :internal_damage_judge, :action => :judge_when_edit
        end
      end
      
      #诊断终了时转入orders一览页面，在转往该页面的函数里将做session的清理工作
      
      def termination
        redirect_to :controller => :orders
      end
    end
